package com.erhuogo.action;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.MemCommoBean;
import com.erhuogo.bean.PageBean;
import com.erhuogo.bean.Response;
import com.erhuogo.dao.MemberDao;
import com.erhuogo.entity.CommodityInfo;
import com.erhuogo.entity.Favorite;
import com.erhuogo.entity.Member;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.CommodityInfoService;
import com.erhuogo.service.MemberService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 商品信息Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:30:42
 */
public class CommodityInfoAction extends ActionSupport {
	private static final long serialVersionUID = 5794342322731076484L;
	private static Logger logger = LoggerFactory.getLogger(CommodityInfoAction.class);
	
	// 模型驱动使用的对象
	private CommodityInfo commodityInfo;
	private Member member;
	private PageBean pageBean;
	private MemCommoBean memCom; //会员商品类
	private String commId; //商品ID
	private String memberId; //登录之后用户的ID
	
	public String getCommId() {
		return commId;
	}

	public void setCommId(String commId) {
		this.commId = commId;
	}

	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public Member getMember() {
		return member;
	}
	
	public void setMember(Member member) {
		this.member = member;
	}
	public MemCommoBean getMemCom() {
		return memCom;
	}

	public void setMemCom(MemCommoBean memCom) {
		this.memCom = memCom;
	}



	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public CommodityInfo getCommodityInfo() {
		return commodityInfo;
	}

	public void setCommodityInfo(CommodityInfo commodityInfo) {
		this.commodityInfo = commodityInfo;
	}
	
	// 注入CommodityInfoService
	private CommodityInfoService commodityInfoService;
	private MemberService memberService;

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public CommodityInfoService getCommodityInfoService() {
		return commodityInfoService;
	}

	public void setCommodityInfoService(CommodityInfoService commodityInfoService) {
		this.commodityInfoService = commodityInfoService;
	}
	
	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}
	
	/**
	 * 1.到发布二货页面
	 * @return
	 */
	public String toPublish(){
		Member existMember = (Member) ServletActionContext.getRequest().getSession().getAttribute("existUser");
		if(existMember!=null){
			return "publishPage";
		}else{
			return "toLogin";
		}
	}
	
	/**
	 * 2.发布二货商品
	 * @return
	 */ 
	public String publish(){
		String title = ServletActionContext.getRequest().getParameter("title");
		String detail = ServletActionContext.getRequest().getParameter("detail");
		String oriPriceStr = ServletActionContext.getRequest().getParameter("oriPrice");
		String transPriceStr = ServletActionContext.getRequest().getParameter("transPrice");
		String quality = ServletActionContext.getRequest().getParameter("quality");
		String provinceId = ServletActionContext.getRequest().getParameter("provinceId");
		String cityId = ServletActionContext.getRequest().getParameter("cityId");
		String schoolId = ServletActionContext.getRequest().getParameter("schoolId");
		String contactWay = ServletActionContext.getRequest().getParameter("contactWay");
		String tradeWay = ServletActionContext.getRequest().getParameter("tradeWay");
		String oneCategoryId = ServletActionContext.getRequest().getParameter("oneCategoryId");
		String twoCatetoryId = ServletActionContext.getRequest().getParameter("twoCategoryId");
		String threeCategoryId = ServletActionContext.getRequest().getParameter("threeCategoryId");
		String headImg = ServletActionContext.getRequest().getParameter("headImg");
		String imgList = ServletActionContext.getRequest().getParameter("imgList");
		String location = ServletActionContext.getRequest().getParameter("location");
		String oneCategoryName = ServletActionContext.getRequest().getParameter("oneCategoryName");
		String twoCategoryName = ServletActionContext.getRequest().getParameter("twoCatetoryName");
		String threeCategoryName = ServletActionContext.getRequest().getParameter("threeCategoryName");
		String memberId = ServletActionContext.getRequest().getParameter("memberId");
		BigDecimal oriPrice = BigDecimal.valueOf(Long.parseLong(oriPriceStr));
		BigDecimal transPrice = BigDecimal.valueOf(Long.parseLong(transPriceStr));
		CommodityInfo commodityInfo = new CommodityInfo(title,detail,headImg,imgList,oriPrice,transPrice,Integer.valueOf(quality),location,
				contactWay,Integer.valueOf(tradeWay),Integer.valueOf(memberId),oneCategoryName,twoCategoryName,threeCategoryName,
				Integer.valueOf(oneCategoryId),Integer.valueOf(twoCatetoryId),Integer.valueOf(threeCategoryId),Integer.valueOf(provinceId),
				Integer.valueOf(cityId),Integer.valueOf(schoolId));
		try {
			commodityInfoService.saveCommodityInfo(commodityInfo);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("发布成功");
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			response.setMessage("发布失败");
			logger.info("发布二货发生异常");
			e.printStackTrace();
		} finally {
			return "publish";
		}
	}
	
	/**
	 * 3.根据名字模糊查询二货商品
	 * @return
	 */
	public String search(){
		String pageNum = ServletActionContext.getRequest().getParameter("pageNum");
		pageBean= commodityInfoService.searchCommodityByName(commodityInfo.getName(), pageNum);
		if (pageBean != null){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("找到符合要求的商品");
		} else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("未找到符合要求的商品");
		}
		return "commodityListPage";
		
	}
	
	/**
	 * 4.根据商品id查询出该商品的详情
	 * @return
	 */
	public String viewDetails(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String commId=request.getParameter("commId");
		String commMemId=request.getParameter("commMemberId");
		commodityInfo=commodityInfoService.getCommById(commId);
		member=memberService.getMemberById(commMemId);
		MemCommoBean memComm=new MemCommoBean(member, commodityInfo);
		request.setAttribute("memComm", memComm);
		return "commodityDetailPage";
	}
	
	public String clickCommend(){
		commodityInfoService.clickCommend(commodityInfo.getId()+"");
		return "clickCommend";
	}
	
	/*判断用户是否已经收藏商品*/
	public String judgeCollect(){
		boolean flag=commodityInfoService.judgeCollect(memberId, commId);
		if(flag){
			response.setMessage("您已收藏过该商品。");
			return "judgeCollect";
		}else{
			return "collectComm";
		}
	}
	
	public String collectComm(){
		Favorite favorite=new Favorite();
		favorite.setAccountId(Integer.parseInt(memberId));
		favorite.setCommodityId(Integer.parseInt(commId));
		favorite.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		boolean flag=commodityInfoService.collectComm(favorite);
		if(flag){
			response.setMessage("收藏成功！");
		}		
		return "collectSuccess";
	}
	
	public String cancelCollect(){
		boolean flag=commodityInfoService.cancelComm(commId, memberId);
		if(flag){
			response.setMessage("取消收藏成功！");
		}
		return "cancelCollect";
	}
	
	/**
	 * 5.分页条件查询二货信息
	 * @return
	 */
	public String listPage(){
		String page = ServletActionContext.getRequest().getParameter("page");
		String rows = ServletActionContext.getRequest().getParameter("rows");
		String id = ServletActionContext.getRequest().getParameter("id");
		String oneCategoryId = ServletActionContext.getRequest().getParameter("oneCategoryId");
		String twoCategoryId = ServletActionContext.getRequest().getParameter("twoCategoryId");
		String threeCategoryId = ServletActionContext.getRequest().getParameter("threeCategoryId");
		String provinceId = ServletActionContext.getRequest().getParameter("provinceId");
		String cityId = ServletActionContext.getRequest().getParameter("cityId");
		String schoolId = ServletActionContext.getRequest().getParameter("schoolId");
		String tradeWay = ServletActionContext.getRequest().getParameter("tradeWay");
		String reportStatus = ServletActionContext.getRequest().getParameter("reportStatus");
		String shieldStatus = ServletActionContext.getRequest().getParameter("cityId");
		String tradeStatus = ServletActionContext.getRequest().getParameter("tradeStatus");
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");
		
		GridPageBean<CommodityInfo> listPage = commodityInfoService.findByPage(page, rows, id, oneCategoryId, twoCategoryId ,threeCategoryId,
				provinceId, cityId, schoolId, tradeWay, reportStatus, shieldStatus, tradeStatus, startDate, endDate);
		if(listPage!=null && listPage.getRecords()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		response.setData(listPage);
		return "listPageSUCCESS";
	}
	
	/**
	 * 6.开启或者屏蔽二货类信息
	 * @return
	 */
	public String updateStatusById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String status = ServletActionContext.getRequest().getParameter("status");
		try {
			commodityInfoService.updateStatus(id,status);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("更新状态成功");
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			response.setMessage("更新状态失败");
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "updateStatusSUCCESS";
	}
}
